import axios from 'axios'
import Vue from 'vue'
import router from '../router/index'
const baseURL = 'http://localhost:3000/admin/api'

const server = axios.create({
  baseURL,
  timeout: 50000
})

server.interceptors.request.use(config => {
  if (localStorage.getItem('admin_token')) {
    config.headers.Authorization = 'Bearer ' + localStorage.getItem('admin_token')
  }
  return config
}, err => {
  
  return Promise.reject(err)
})

server.interceptors.response.use(res => {
  return res
}, err => {
  if (err.response.status === 401) {
    router.push('/login')
  }
  if (err.response.data.message) {
    Vue.prototype.$message({
      type: 'error',
      message: err.response.data.message
    })
  }
  return Promise.reject(err)
})

export default server